//
//  RefuseViewController.m
//  work
//
//  Created by 徐宏飞 on 16/6/3.
//  Copyright © 2016年 方伟. All rights reserved.
//

#import "RefuseViewController.h"
#import "RefuseCollectionViewCell.h"



static NSString *refuseCollectionViewCell_ID = @"RefuseCollectionViewCell";



@interface RefuseViewController ()<
UICollectionViewDelegate,
UICollectionViewDataSource,
UITextViewDelegate
>

@property (weak, nonatomic) IBOutlet UICollectionView *refuseReasonCV;  //原因collectionView
@property (weak, nonatomic) IBOutlet UITextView *seasonTextView;        //原因输入
@property (nonatomic, strong) NSMutableArray *selectArray;              //选择的原因
@property (nonatomic, strong)UILabel *uilabel;
@property (nonatomic, strong) NSArray *dataArray;              //原因
@property (nonatomic, copy)NSString *resonText;


@end

@implementation RefuseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"拒单";
    
    _selectArray = [NSMutableArray array];
    [self _configurationRefuseReasonCV];
    _dataArray=@[@"时间冲突",@"准备休息",@"距离太远",@"价格太低",@"工期太紧"];

}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"Refuse"];//("PageOne"为页面名称，可自定义)
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"Refuse"];
}

- (void) _configurationRefuseReasonCV{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    //item
    CGSize size = CGSizeMake(115, 30);
    
    //4  4s  5  5s
    if (SCREEN_WIDTH == 320) {
        size = CGSizeMake(93, 30);
    }
    
    //6 6s
    if (SCREEN_WIDTH == 375) {
        size = CGSizeMake(105, 30);
    }
    
    
    
    flowLayout.itemSize = size;
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 0, 0, 0);
    flowLayout.minimumLineSpacing = 10;
    flowLayout.minimumInteritemSpacing = 5;
    
    
    _refuseReasonCV.collectionViewLayout = flowLayout;
    
    //注册item
    [_refuseReasonCV registerNib:[UINib nibWithNibName:@"RefuseCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:refuseCollectionViewCell_ID];
    
    _refuseReasonCV.allowsMultipleSelection = YES;
    
    
    
}






#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    RefuseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:refuseCollectionViewCell_ID forIndexPath:indexPath];
    
    
    cell.layer.cornerRadius = 4;
    [cell.layer masksToBounds];
    cell.label.text = _dataArray[indexPath.row];
    
    
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = COLOR_THEHE;
    
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    [_selectArray addObject:_dataArray[indexPath.row]];
    
    self.resonText = [_selectArray componentsJoinedByString:@","];
    
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{

    if ([_selectArray containsObject:_dataArray[indexPath.row]]) {
        [_selectArray removeObject:_dataArray[indexPath.row]];
    }
    self.resonText = [_selectArray componentsJoinedByString:@","];
    


    

}




#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView{
    

    NSLog(@"%@", textView.text);
}








#pragma mark - event response
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}


- (IBAction)btnUpLoad:(id)sender {
    [self.view endEditing:YES];
    
    if (self.resonText.length>0) {
        
        _seasonTextView.text = [_seasonTextView.text stringByAppendingString:self.resonText];
    }
    
    [self performSelector:@selector(delayMethod) withObject:nil afterDelay:0.3f];
    

    
}

-(void)delayMethod{

    if (_seasonTextView.text.length == 0) {
        
        [self showAlertWithTitle:@"温馨提示" message:@"请填写拒单原因" cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];
        return;
    }
    
    
    if ([self.delegate respondsToSelector:@selector(returnReson:withOrdID:)]) {
        [self.delegate returnReson:_seasonTextView.text withOrdID:_orderID];
    }
    [self.navigationController popViewControllerAnimated:YES];


}












- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
